package ch.wanandroid.lib.banner;


import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import ch.wanandroid.comr.lib.R;

/**
 * @author chnehao
 * @datetime: 2018/10/9 16:15
 * @description:
 */
public class SimpleImageBanner extends BaseIndicatorBanner<BannerItem,SimpleImageBanner> {

    public SimpleImageBanner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public View onCreateItemView(int position) {
        View view = View.inflate(mContext, R.layout.adapter_simple_image, null);
        ImageView iv = view.findViewById(R.id.iv);
        final BannerItem item = mDatas.get(position);
        int itemWidth = mDisplayMetrics.widthPixels;
        //int itemHeight = (int) (itemWidth * 360 * 1.0f / 640);
        int itemHeight = (int) (itemWidth * 500 * 1.0f / 900);
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setLayoutParams(new LinearLayout.LayoutParams(itemWidth, itemHeight));
        String imgUrl = item.imgUrl;
        if (!TextUtils.isEmpty(imgUrl)) {
            Glide.with(mContext)
                    .load(imgUrl)
                    .override(itemWidth, itemHeight)
                    .centerCrop()
                    .placeholder(R.drawable.network_error_illu)
                    .into(iv);
        } else {
            iv.setImageResource(R.drawable.network_error_illu);
        }
        return view;
    }

    @Override
    public void onTitleSlect(TextView tv, int position) {
        final BannerItem item = mDatas.get(position);
        tv.setText(item.title);
    }

}
